package edu.hziee.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import edu.hziee.dao.CourseDao;
import edu.hziee.dao.StudentCourseDao;
import edu.hziee.models.Course;
import edu.hziee.models.Student;
import edu.hziee.models.StudentCourse;


@Controller
@RequestMapping("/course")
public class CourseController {

	@Autowired
	private CourseDao coursedao;
	
	@Autowired
	private StudentCourseDao scdao;
	
	@RequestMapping(value="/sclist",method=RequestMethod.GET)
	public ModelAndView findAll(){
		
		ModelAndView mav=new ModelAndView("student/studentList");
		List<StudentCourse> studentcourses = scdao.findAll();
		mav.addObject("studentcourses",studentcourses);
		return mav;

	}
	
	
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView get(@RequestParam int sid){
		ModelAndView mav=new ModelAndView("course/course");
		List<StudentCourse> studentcourses = scdao.findById(sid);
		mav.addObject("studentcourses",studentcourses);
		return mav;
	}
	
	
	@RequestMapping(value="/clist",method=RequestMethod.GET)
	public ModelAndView getAll(){
		ModelAndView mav=new ModelAndView("course/courseList");
		List<Course> courses = coursedao.findAll();
		mav.addObject("courses",courses);
		return mav;

	}
	
	@RequestMapping(value="/cadd",method=RequestMethod.GET)
	public ModelAndView add(){
		ModelAndView mav=new ModelAndView("course/addcourse");
		return mav;
		
	}
	@RequestMapping(value="/cadd",method=RequestMethod.POST)
	public ModelAndView add(
			@RequestParam int cid,
			@RequestParam String clnum,
			@RequestParam String cname,
			@RequestParam String credit,
			@RequestParam int capacity
			){
		Course course = new Course();
		course.setCid(cid);
		course.setClnum(clnum);
		course.setCname(cname);
		course.setCredit(credit);
		course.setCapacity(capacity);
		coursedao.addCourse(course);
		
		ModelAndView mav=new ModelAndView("redirect:clist");
		return mav;
	}
	
	@RequestMapping(value="/editcourse",method=RequestMethod.GET)
	public ModelAndView edit(@RequestParam int cid){
		Course course = coursedao.findById(cid);
		ModelAndView mav=new ModelAndView("course/editcourse");
		mav.addObject("course",course);
		return mav;
	}
	
	@RequestMapping(value="/editcourse",method=RequestMethod.POST)
	public ModelAndView edit(
			@RequestParam int cid,
			@RequestParam String clnum,
			@RequestParam String cname,
			@RequestParam String credit,
			@RequestParam int capacity
			){
		Course course = new Course();
		course.setCid(cid);
		course.setClnum(clnum);
		course.setCname(cname);
		course.setCredit(credit);
		course.setCapacity(capacity);
		
		coursedao.modify(course);
		ModelAndView mav=new ModelAndView("redirect:clist");
		return mav;
	}
	
	
	@RequestMapping(value="/delete",method=RequestMethod.GET)
	public ModelAndView delete(@RequestParam int cid){
		coursedao.delete(cid);
		ModelAndView mav=new ModelAndView("redirect:clist");
		return mav;
	} 
	

}
